import { RealTimeMsg } from '@/api/custcare/talk/BackUserModel'
import { getCustcareToken } from '@/utils/custcareAuth'
import { CustcareWs } from '@/utils/websocket'
import { defineStore } from 'pinia'
import { reactive } from 'vue'

export type CustCareUserState = {
    socket: any
    realTimeMsg: RealTimeMsg
}

export const custCareUserStore = defineStore('custCareUserStore', {
    state: (): CustCareUserState => {
        return {
            socket: Object,
            realTimeMsg: reactive<RealTimeMsg>({
                channel_id: '',
                content: '',
                send_time: '',
                to_user_uuid: '',
                type: 1,
                user_uuid: ''
            })
        }
    },
    actions: {
        setSocket() {
            let token = getCustcareToken() || ''
            this.socket = new CustcareWs(
                <string>import.meta.env.VITE_CUSTCARE_WS_SERVER,
                token
            )
            console.log('socket=', this.socket)
        },
        setRealTimeMsg(param: RealTimeMsg) {
            this.realTimeMsg.channel_id = param.channel_id
            this.realTimeMsg.content = param.content
            this.realTimeMsg.send_time = param.send_time
            this.realTimeMsg.to_user_uuid = param.to_user_uuid
            this.realTimeMsg.type = param.type
            this.realTimeMsg.user_uuid = param.user_uuid
        }
    },
    getters: {
        getSocket(state) {
            return state.socket
        },
        getRealTimeMsg(state) {
            return state.realTimeMsg
        }
    }
})
